package me.filter;

import javax.servlet.*;
import java.io.IOException;

//@WebFilter(urlPatterns="/*",initParams = {@WebInitParam(name="encoding",value = "UTF-8")})
public class EncodingFilter implements Filter {

    //初始化编码方式
    String encoding = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //通过注解获取编码方式
        encoding = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //设置编码方式
        servletRequest.setCharacterEncoding(encoding);
        //程序继续执行
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
}
